package com.cooper.controllers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.cooper.constants.DisTypeConstants;
import com.cooper.entity.DisplayPicConfigListDo;
import com.cooper.pagination.PageInfo;
import com.cooper.pagination.PageResult;
import com.cooper.service.admin.DisplayPicConfigService;
import com.cooper.service.item.ItemInfoService;
import com.cooper.vo.CompositeDesignerItemInfoVo;
import com.cooper.vo.DisplayPicConfigVo;

import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.portal.Portal;

public class DefaultController {
	
	@Autowired
	private DisplayPicConfigService displayPicConfigService;

	@Autowired
	private ItemInfoService itemInfoService;
	
	@Get
	public String execute(Invocation inv, Portal portal) {
		DisplayPicConfigVo queryParamVo = new DisplayPicConfigVo();
		queryParamVo.setStatus(DisplayPicConfigListDo.DISPLAY_YES);
		queryParamVo.setDisType(DisTypeConstants.INDEX_HEAD);
		PageInfo pageInfo = new PageInfo();
		pageInfo.setPageIndex(1);
		pageInfo.setPageSize(6);
		PageResult<DisplayPicConfigVo> headpageResult = this.displayPicConfigService.queryDisplayPicConfigPage(queryParamVo, pageInfo);
		if(null != headpageResult){
			inv.addModel("indexHeadList", headpageResult.getResult());
		}
		
		try {
			this.prepareHotSales(inv);
		} catch (Exception e) {
		}
		
		DisplayPicConfigVo adveQueryParam = new DisplayPicConfigVo();
		adveQueryParam.setStatus(DisplayPicConfigListDo.DISPLAY_YES);
		adveQueryParam.setDisType(DisTypeConstants.INDEX_ADVERTISEMENT);
		PageInfo advepageInfo = new PageInfo();
		advepageInfo.setPageSize(1);
		PageResult<DisplayPicConfigVo> advepageResult = this.displayPicConfigService.queryDisplayPicConfigPage(adveQueryParam, advepageInfo);
		if(null != advepageResult){
			inv.addModel("indexAdveList", advepageResult.getResult());
		}
		
		portal.addWindow("hotShops", "/getHotShops");
		return "home";
	}
	
	public void prepareHotSales(Invocation inv) throws Exception {
		DisplayPicConfigVo queryParamVo = new DisplayPicConfigVo();
		queryParamVo.setStatus(DisplayPicConfigListDo.DISPLAY_YES);
		queryParamVo.setDisType(DisTypeConstants.INDEX_HOT_SALES);
		PageInfo pageInfo = new PageInfo();
		pageInfo.setPageIndex(1);
		pageInfo.setPageSize(10);
		
		PageResult<DisplayPicConfigVo> ret = displayPicConfigService.queryDisplayPicConfigPage(queryParamVo, pageInfo);
		List<String> itemIds = new ArrayList<String>();
		Map<String,String> data = new HashMap<String,String>();
		Map<String,String> parths = new HashMap<String,String>();
		Map<String,String> customUrls = new HashMap<String,String>();
		if(ret!=null&&ret.getResult()!=null){
			for(DisplayPicConfigVo item:ret.getResult()){
				String path = item.getPath();
				String temp = path.substring(path.indexOf("?")+1);
				String[] arr = temp.split("=");
				if(arr!=null&&arr.length==2){
					itemIds.add(arr[1]);
					data.put(arr[1],item.getSort()+"");
					customUrls.put(arr[1], item.getFileId());
					parths.put(arr[1], item.getPath());
				}
				
			}
			if(itemIds!=null&&!itemIds.isEmpty()){
				List<CompositeDesignerItemInfoVo> items = itemInfoService.findCompositeDesignerItemInfosByItemIds(itemIds);
			    if(items!=null&&!items.isEmpty()){
			    	for(CompositeDesignerItemInfoVo item:items){
			    		item.setCustomUrl(customUrls.get(item.getId()+""));
			    		item.setPath(parths.get(item.getId()+""));
			    		inv.addModel("hotsale"+data.get(item.getId()+""), item);
			    	}
			    }
			}
		}
		
	}
}
